/**
 * @class Menu
 * @extends Ext.Controller
 * The Menu controller
 */

Ext.regController('Menu', {

	/*
	* Summary:		Initialization controller 
	* 				(note: cannot use init name - it's controller used before launch application function) 
	* Parameters:	none
	*/
	
	initController : function(){
	
		Ext.dispatch({
			controller: 'Cart',
			action: 'initController'
		});
	},
	
	
	/*
	* Summary:		Function shows menu tab, if there are depths in url there are two ways: 
	* 				1) the page is just refreshed - nested list cannot call getNode function, so the url is cutted and the main menu is shown
	* 				2) the url changed after menu navigation - getNode works ok
	* 				Function simulates itemtap event on nestedList component to go through the list
	* Parameters:	setCard (boolean) - decides to change active tab | id1 - first depth in url and nested list | id2 - second depth
	*/
	
	showMenu : function(p){
		var nl = RefApp.views.menuCard;

		if(p.setCard !== false) 
			RefApp.views.viewport.setActiveItem(0);	// switch tab
		nl.setRootActive();							// show root panel in nestedList		
		nl.title = "Menu";							//change title (caused by changing title for leaves)
		nl.syncToolbar(nl.items.item[0]);			//refresh toolbar
		
		// go deeper in nestedList
		if(p.id1){
			var ids = new Array(p.id1);
			
			// second depth
			if(p.id2)
				ids.push(p.id2);
			
			for(i=0; i<ids.length; i++)
			{
				active = nl.getActiveItem();
				next = active.getNode(parseInt(ids[i],10));
				
				// if getNode is defined
				if(next)
					nl.fireEvent('itemtap', active, ids[i], next);
				else
					this.addressCut();
			}
		}
	},
	
	/*
	* Summary:		Function shows promotions tab. 
	* Parameters:	setCard (boolean) - decides to change active tab | id1 - first depth in url and nested list | id2 - second depth
	*/
	
	showPromotions : function(p){
		if(p.setCard !== false) RefApp.views.viewport.setActiveItem(2);	// switch tab
		p.setCard = false;
		this.showMenu(p);
	},
	
	/*
	 * Summary:		Function shows about tab.
	 * Parameters:	setCard (boolean) - decides to change active tab
	 */
	showAbout : function(p){
		if(p.setCard !== false) RefApp.views.viewport.setActiveItem(3);	// switch tab
	},
	
	/*
	* Summary:		Function shows cart tab. 
	* Parameters:	setCard (boolean) - decides to change active tab
	*/
	
	showCart : function(p){
		if(p.setCard !== false) RefApp.views.viewport.setActiveItem(1);	// switch tab
		Ext.dispatch({							// refresh cart
			controller: 'Cart',
			action: 'refreshCart'
		});
		Ext.dispatch({							
			controller: 'Cart',
			action: 'showCartList'
		});
	},
	
	/*
	* Summary:		Function increases badge text in cart icon by one. 
	* Parameters:	none
	*/
	
	increaseBadge : function(){
		var carttab = RefApp.views.cartCard.tab,					// reference to cartCard.tab
			badgenumber = parseInt(carttab.badgeText),	
			nextnumber = isNaN(badgenumber) ? 1 : badgenumber+1;	// increasing number or set it to one
		
		carttab.setBadge(nextnumber);								// save nextnumber to badgeText
	},
	
	/*
	* Summary:		Function refresh badge text 
	* Parameters:	newBadge (type: int)
	*/
	
	refreshBadge : function(){
		var newBadge = RefApp.stores.cartStore.data.length;
		RefApp.views.cartCard.tab.setBadge((newBadge == 0) ? "" : newBadge); // save nextnumber to badgeText
	},
	
	/*
	* Summary:		Function resets badge text in cart icon. 
	* Parameters:	none
	*/
	
	resetBadge : function(){
		RefApp.views.cartCard.tab.setBadge("");
	},
	
	/*
	* Summary:		Add item to the cart and proceed to order form
	* Parameters:	item: item to add | size: size of the item
	*/
	
	orderItem : function(p){
		Ext.dispatch({									// add item to the cart
			controller: 'Cart',
			action: 'addItem',
			item: p.item, 
			size: p.size
		});	
		this.showCart({setCard: true});								// show cart tab
		Ext.dispatch({									// show form
			controller: 'Cart',
			action: 'showForm'
		});
	},
	
	/*
	 * Summary:		Show menu pop-up panel
	 * Parameters:	itemData: item chosen by user | size: size of the item
	 */
	
	showPopup: function(p){
		var pop = new RefApp.views.MenuPopup({size: p.size, itemData: p.itemData});
		pop.show('pop');
	},
	
	/*
	 * Summary:		Function cut last parameter from the URL address
	 * Parameters:	none
	 */
	addressCut : function(){
		var hist = Ext.History,
			address = hist.getToken().split("/");
		address.pop();
		newAddress = address.join("/");
		hist.add(newAddress);	
	}
	
});